package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;


    @Autowired
    ProductConverter productConverter;

    /**
     * @param id
     * @return
     * @name yjc
     */
    @Override
    public ProductDetailDto getProductDetail(Long id) {
        Item item = itemMapper.selectByPrimaryKey(id);
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        ProductDetailDto productDetailDto = productConverter.toProductDetailDto(item, itemDesc);
        return productDetailDto;
    }

    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {
        if (pageInfo.getCid() == null) {
            if (pageInfo.getSort().equals("-1")){
                List<Item> item1 = itemMapper.selectALLByUp();
                return getAllProductResponse(pageInfo, item1);
            }
            else if (pageInfo.getSort().equals("1")){
                List<Item> item1 = itemMapper.selectALLByDowm();
                return getAllProductResponse(pageInfo, item1);
            }
            else{
                List<Item> items1 = itemMapper.selectAll();
                return getAllProductResponse(pageInfo, items1);
            }
        }else {
            List<Item> items1 = itemMapper.selectByProductID(pageInfo.getCid());
            return getAllProductResponse(pageInfo, items1);
        }
    }

    private AllProductResponse getAllProductResponse(PageRequest pageInfo, List<Item> items1) {
        List<ProductDto> list = productConverter.items2Dto(items1);
        AllProductResponse allProductResponse = new AllProductResponse();
        allProductResponse.setData(list);
        allProductResponse.setTotal((long) pageInfo.getPage());
        return allProductResponse;
    }

    @Override
    public Set<PanelDto> getRecommendGoods() {
        return null;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }
}
